package com.maszy.commons.tools;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

public class Commons {

	public static String setUploadPath(String dirName) {
		// 上传文件存放目录
		createFolder(dirName);
		return dirName;
	}

	public static boolean createFolder(String folderPath) {
		boolean b = false;
		try {
			File filePath = new File(folderPath);
			if (!filePath.exists()) {
				b = filePath.mkdir();
				// System.out.println(b);
				// System.out.println(filePath+"新建目录成功");
			} else {
				// System.out.println("目录已存在");
			}
		} catch (Exception e) {
			// System.out.println("新建目录出错");
			e.printStackTrace();
		}
		return b;
	}

	public static boolean fileIsIn(String str) {
		boolean b = true;
		File f = new File(str);
		if (!f.exists()) {
			b = false;
			System.out.println("文件:" + str + "===>不存在");
		} else {
			b = true;
			System.out.println("文件:" + str + "===>已存在");
		}
		return b;
	}

	public static String FillSpace(String str, int length) {
		if (str != null) {
			int valueLength=valueLength(str);
			while (length - valueLength > 0) {				
				str = str + " ";
				valueLength=valueLength(str);
			}
		}
		return str;
	}
	public static int valueLength(String str){
		int valueLength = 0;
		String chinese = "[\u0391-\uFFE5]";
		/* 获取字段值的长度，如果含中文字符，则每个中文字符长度为2，否则为1 */
		for (int i = 0; i < str.length(); i++) {
			/* 获取一个字符 */
			String temp = str.substring(i, i + 1);
			/* 判断是否为中文字符 */
			if (temp.matches(chinese)) {
				/* 中文字符长度为2 */
				valueLength += 2;
			} else {
				/* 其他字符长度为1 */
				valueLength += 1;
			}
		}
		return valueLength;
	}
}

